(function (angular) {
    'use strict';

    angular.module('ytong.controllers').controller('feedbackCtrl',feedbackCtrl);

    function feedbackCtrl(feedbacksManager, ModalService, Constants, usersManager){


        var OPEN = "0";
        var REVIEWING = "2";

        var _self = this;

        this.feedbacks = feedbacksManager.getFeedbacks();
        _self.showAll = false;

        this.getFeedbackUsername = function(feedback){
            return usersManager.getUsername(feedback.user);
        };

        this.handleFeedback = function(feedback){
            ModalService.showModal({
                templateUrl: 'handleFeedback',
                controller: 'handleFeedbackCtrl as hfc',
                inputs:{
                    feedback: feedback
                }
            })
                .then(function(modal){
                    modal.element.modal();
                    modal.close.then(function(result){

                        if(result.action === Constants.COMMANDS.UPDATE){
                            feedbacksManager.updateFeedback(result.feedback);
                        }
                    });
                });
        };

        this.filterFeedbacks = function(feedback){
            if(_self.showAll){
                return true;
            }

            if((feedback.status === OPEN) || (feedback.status === REVIEWING)){
                return true;
            }

            return false;
        }

    };

    angular.module('ytong.controllers').controller('handleFeedbackCtrl', handleFeedbackCtrl);

    function handleFeedbackCtrl(close, feedback){

        this.feedback = angular.copy(feedback);

        this.close = function(action) {
            var result = {};
            result.action = action;
            result.feedback = this.feedback;
            close(result, 500); // close, but give 500ms for bootstrap to animate
        };

    };

}(angular));